#include "Input.h"

namespace DirectXFramework
{
	namespace Input
	{
		//////////////
		// Keyboard //
		//////////////

		GameKeyboard::GameKeyboard (void)
		{
		}

		GameKeyboard::GameKeyboard (HINSTANCE hInstance, HWND hWnd)
		{
			this->Init(hInstance, hWnd);
		}

		GameKeyboard::~GameKeyboard (void)
		{
			if (this->Keyboard)
				this->Keyboard->Unacquire();
			if (this->Interface)
				this->Interface->Release();
		}

		BOOL GameKeyboard::Init (HINSTANCE hInstance, HWND hWnd)
		{
			BOOL Success = TRUE;

			DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&Interface, NULL);
			if (!this->Interface)
				Success = FALSE;
			else
			{
				this->Interface->CreateDevice(GUID_SysKeyboard, &Keyboard, NULL);
				if (!this->Keyboard)
					Success = FALSE;
				else
				{
					this->Keyboard->SetDataFormat(&c_dfDIKeyboard);
					this->Keyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
				}
			}

			return Success;
		}

		void GameKeyboard::Update (void)
		{
			this->Keyboard->Acquire();
			this->Keyboard->GetDeviceState(256, (void*)KeyStates);
		}

		BOOL GameKeyboard::IsKeyDown (DWORD Key)
		{
			BOOL Success = FALSE;
			if (this->KeyStates[Key] & 0x80)
				Success = TRUE;
			return Success;
		}

		///////////
		// Mouse //
		///////////

		GameMouse::GameMouse (void)
		{
		}

		GameMouse::GameMouse (HINSTANCE hInstance, HWND hWnd)
		{
			this->Init(hInstance, hWnd);
		}

		GameMouse::~GameMouse (void)
		{
			if (this->Mouse)
				this->Mouse->Unacquire();
			if (this->Interface)
				this->Interface->Release();
		}

		BOOL GameMouse::Init (HINSTANCE hInstance, HWND hWnd)
		{
			BOOL Success = TRUE;

			DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&Interface, NULL);
			if (!this->Interface)
				Success = FALSE;
			else
			{
				this->Interface->CreateDevice(GUID_SysMouse, &Mouse, NULL);
				if (!this->Mouse)
					Success = FALSE;
				else
				{
					this->Mouse->SetDataFormat(&c_dfDIMouse);
					this->Mouse->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
				}
			}

			return Success;
		}

		BOOL GameMouse::IsMouseClicked (DWORD Button)
		{
			BOOL Success;
			if (this->MouseStates.rgbButtons[Button] & 0x80)
				Success = TRUE;
			return Success;
		}

		LONG GameMouse::GetMouseXChange (void)
		{
			return this->MouseStates.lX;
		}

		LONG GameMouse::GetMouseYChange (void)
		{
			return this->MouseStates.lY;
		}

		LONG GameMouse::GetMouseWheelChange (void)
		{
			return this->MouseStates.lZ;
		}
	};
};